package bb.lanxing.net.okhttp;

import bb.lanxing.net.core.ApiCookie;
import bb.lanxing.net.core.ApiCookieProvider;
import java.util.ArrayList;
import java.util.List;
import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.HttpUrl;

public class OkCookieJarImpl implements CookieJar {
    private ApiCookieProvider provider;

    public OkCookieJarImpl(ApiCookieProvider provider) {
        this.provider = provider;
    }

    @Override
    public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
        if (this.provider != null) {
            ArrayList arrayList = new ArrayList();
            if (cookies != null) {
                for (Cookie cookie : cookies) {
                    arrayList.add(new ApiCookie.Builder().domain(cookie.domain()).expiresAt(cookie.expiresAt()).name(cookie.name()).path(cookie.path()).build());
                }
            }
            this.provider.saveFromResponse(url.toString(), arrayList);
        }
    }

    @Override
    public List<Cookie> loadForRequest(HttpUrl url) {
        List<ApiCookie> loadForRequest;
        ArrayList arrayList = new ArrayList();
        ApiCookieProvider apiCookieProvider = this.provider;
        if (apiCookieProvider != null && (loadForRequest = apiCookieProvider.loadForRequest(url.toString())) != null) {
            for (ApiCookie apiCookie : loadForRequest) {
                arrayList.add(new Cookie.Builder()
                        .domain(apiCookie.domain())
                        .expiresAt(apiCookie.expiresAt()).name(apiCookie.name()).path(apiCookie.path()).build());
            }
        }
        return arrayList;
    }
}
